/***********************************
 * SubTxt - Main Activity - Entry Point for user
 *  After the ToDoList and WalkingTour, this class is pretty much old hat
 *  nothing too new or exciting here except the extended simple cursor adapter
 * 
 */
package edu.csuohio.mad.ssmk.subtxt;

import android.net.Uri;
import android.os.Bundle;
import android.app.ListActivity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class SubTxt extends ListActivity implements
		LoaderManager.LoaderCallbacks<Cursor> {

	private Uri uriSMSInbox = Uri.parse("content://sms/inbox");
	private SMSCursorAdapter adapter;
	private static final int SMS_LOADER_ID = 0;
	private static final String[] PROJECTION = new String[] { "_id", "address",
			"body", "date", "read" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_subtxt);

		Cursor cur = getContentResolver().query(uriSMSInbox, PROJECTION, null,
				null, null);

		String[] proj = new String[] { "address", "date" };
		int[] viewIDs = new int[] { R.id.sender, R.id.senttime };

		// using our own cursor adapter lets us customize the views to our
		// hearts content, as opposed to the direct binding that the default
		// adapter provides
		adapter = new SMSCursorAdapter(this, R.layout.sms_message, cur, proj,
				viewIDs, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

		setListAdapter(adapter);
		LoaderManager loadMan = getLoaderManager();
		loadMan.initLoader(SMS_LOADER_ID, null, this);
		registerForContextMenu(getListView());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.subtxt_menu, menu);
		return true;
	}

	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.message_menu, menu);
	}

	// spawn the selected sms message in MessageOps if it's clicked on
	protected void onListItemClick(ListView l, View v, int pos, long id) {
		viewSMS(id);
	}

	// method to handle Task selection
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		// compose a new encrypted message
		case R.id.menu_compose:
			Intent createIntent = new Intent(this, MessageOps.class);
			startActivity(createIntent);
			return true;
			// update the settings if we want
		case R.id.menu_settings:
			Intent settingsIntent = new Intent(this, SettingsActivity.class);
			startActivity(settingsIntent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {
		// long pressing will let us view a message
		case R.id.menu_view:
			viewSMS(info.id);
			return true;
			// or we can also delete it
		case R.id.menu_delete:
			deleteSMS(info.id);
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}

	// Spawn an instance of the MessageOps activity to decrypt, as needed, and
	// view an SMS
	private void viewSMS(long id) {
		Intent viewIntent = new Intent(this, MessageOps.class);
		Bundle bundle = new Bundle();
		bundle.putLong("_id", id);
		viewIntent.putExtras(bundle);
		startActivity(viewIntent);
	}

	// deleting a message deletes it directly from the builting android
	// messaging app using the content provider
	private void deleteSMS(long id) {
		Uri smsToDelete = Uri.withAppendedPath(Uri.parse("content://sms/"),
				String.valueOf(id));
		getContentResolver().delete(smsToDelete, null, null);
	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		return new CursorLoader(this, uriSMSInbox, PROJECTION, null, null, null);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
		adapter.swapCursor(arg1);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		adapter.swapCursor(null);
	}
}